import { defineConfig, loadEnv } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from "path";




import VueSetupExtend from 'vite-plugin-vue-setup-extend'
import AutoImport from "unplugin-auto-import/vite"
import Components from 'unplugin-vue-components/vite';
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import { registerRuntimeCompiler } from '@vue/runtime-core';

import autoprefixer from 'autoprefixer'


export default defineConfig((mode, command) => {

  const env = loadEnv(mode, process.cwd())
  const { VITE_APP_ENV } = env

  return {
    base: VITE_APP_ENV === 'production' ? '/' : '/',
    plugins: [vue(),
    AutoImport({
      imports: [
        'vue',
        'vue-router',
        'pinia'
      ],
      dts: false,
      //element
      resolvers: [ElementPlusResolver()],
    }),
    VueSetupExtend(),
    Components({
      //element
      resolvers: [ElementPlusResolver()],
      //默认存放位置
      //dts: "src/components.d.ts",
    }),

    ]
    ,
    build: {
      cssCodeSplit: true,
      // minify: "terser",
      // terserOptions: {
      //   compress: {
      //     //生产环境时移除console
      //     drop_console: false,
      //     drop_debugger: false,
      //   },
      // },
      rollupOptions: {
        output: {
          manualChunks: { //静态资源分拆打包
            vue: ['vue', 'vue-router', 'pinia'],//
            //echarts: ['echarts'],
            elementplus: ['element-plus'],

          }

        },
      },
    },
    resolve: {

      alias: {
        // 设置路径
        '~': resolve(__dirname, './'),
        // 设置别名
        '@': resolve(__dirname, './src')
      },

      extensions: ['.mjs', '.js', '.ts', '.jsx', '.tsx', '.json', '.vue']
    },
    server: {
      port: 82,
      host: true,
      open: true,
      proxy: {
        '/dev-api': {
          target: 'http://127.0.0.1:9000',
          changeOrigin: true,
          rewrite: (p) => p.replace(/^\/dev-api/, '')
        },

      }
    },
    css: {
      postcss: {
        plugins: [

          autoprefixer({  // 自动添加前缀
            overrideBrowserslist: [
              "Android 4.1",
              "iOS 7.1",
              "Chrome > 31",
              "ff > 31",
              "ie >= 8"
              //'last 2 versions', // 所有主流浏览器最近2个版本
            ],
            grid: true
          })
        ]
      },
    }

  }
})
